package com.sqsf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sqsf.javabean.User;
import com.sqsf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
@RequestMapping("/users")

public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",required = false,defaultValue = "1") int page, @RequestParam(value = "limit",required = false,defaultValue = "5")int limit){
        PageHelper.startPage(page, limit);
        List<User> all = userService.findAllUser();
        PageInfo<User> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo",pageInfo);
        return "/users/users-list";
    }
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(HttpServletRequest request){
        return "users/users-add";
    }
    //添加用户
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(User user){
        userService.add(user);
        return "redirect:/users/findAll";
    }

}